home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / internet-tools / connect-line / cl / devkit / c / examples / connectris / connectris.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-06  |  10.8 KB  |  498 lines

  1. //
  2. // Connectris-Kommando für Connectline
  3. //
  4. // Copyright 1995 by Mathias Mischler
  5. //
  6.  
  7. #define HighscoreFilename "CONNECTLINE:prefs/CL-Tris.score"
  8. #define Firstline 4
  9. #define Begin 20
  10. #define Width 15
  11. #define Length 17
  12. #define NextX 45
  13. #define NextY 10
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <cl/clonline.h>
  19.  
  20. // Version String
  21.  
  22. char version[] = { "$VER: Connectris 1.2 " __AMIGADATE__ };
  23.  
  24. //
  25. // Draw_Highscore ()
  26. //
  27.  
  28. void Draw_Highscore ( void )
  29. {
  30. BPTR lock, file;
  31. char spieler [ 10 ][ 40 ];
  32. ulong punkte [ 10 ];
  33. ulong count;
  34. char line [ 82 ];
  35.  
  36.     lock = Lock ( HighscoreFilename, ACCESS_READ );
  37.     if ( lock )
  38.     {
  39.         UnLock ( lock );
  40.         if ( file = Open ( HighscoreFilename, MODE_OLDFILE ) )
  41.         {
  42.             for ( count = 0; count < 10; count ++ )
  43.             {
  44.                 FGets ( file, line, 80 );
  45.                 line [ strlen ( line ) - 1 ] = 0; // LF entfernen
  46.                 sscanf ( line, "%d %s", &punkte [ count ], spieler [ count ] );
  47.             }
  48.             Close ( file );
  49.         }
  50.         else
  51.         {
  52.             PutStr ("Can't open highscore-file.\n");
  53.             return;
  54.         }
  55.     }
  56.     else
  57.     {
  58.         if ( file = Open ( HighscoreFilename, MODE_NEWFILE ) )
  59.         {
  60.             for ( count = 0; count < 10; count ++ )
  61.             {
  62.                 punkte [ count ] = 0;
  63.                 strcpy ( spieler [ count ], "???" );
  64.                 FPuts ( file, "0 ???\n" );
  65.             }
  66.             Close ( file );
  67.         }
  68.         else
  69.         {
  70.             PutStr ("Can't open highscore-file.\n");
  71.             return;
  72.         }
  73.     }
  74.  
  75.     PutMSG ("MSG_HIGHSCORE");
  76.     for ( count = 0; count < 10; count ++ )
  77.     {
  78.         CLONL_Printf ( MSG ("MSG_HIGHSCOREFORMAT"), punkte [ count ], spieler [ count ] );
  79.     }
  80.  
  81.     PutStr ("\n");
  82. }
  83.  
  84. //
  85. // Draw_Playfield ()
  86. //
  87.  
  88. void Draw_Playfield ( void )
  89. {
  90. ulong count;
  91.  
  92.     CLONL_Color ( COLOR_Blue, COLOR_White, STYLE_Inverse );
  93.     for ( count = Firstline; count < Firstline + Length; count ++ )
  94.     {
  95.         CLONL_GotoXY ( Begin - 1,     count ); FPutC ( Output(), '|' );
  96.         CLONL_GotoXY ( Begin + Width, count ); FPutC ( Output(), '|' );
  97.     }
  98.     CLONL_GotoXY ( Begin - 1, Firstline + Length );
  99.     FPutC ( Output(), '+' );
  100.     for ( count = 0; count < Width; count ++ )
  101.     {
  102.         FPutC ( Output(), '-' );
  103.     }
  104.     FPutC ( Output(), '+' );
  105.     CLONL_FlushOutput ();
  106. }
  107.  
  108. char teile [] =
  109. {
  110.     0, 0,  0, 1,  1, 1,  1, 0, // Quader
  111.     0, 0,  0, 1,  1, 1,  1, 2, //
  112.   1, 0,  1, 1,  0, 1,  0, 2, //
  113.     0, 0,  0, 1,  0, 2,  0, 3, // Langer Vierer
  114.     0, 0,  0, 1,  1, 1,  0, 2, // Dreieck
  115.   0, 0,  1, 0,  1, 1,  1, 2, //
  116.   0, 0,  1, 0,  0, 1,  0, 2, //
  117.  
  118.     0, 0,  0, 1,  1, 1,  1, 0, // Quader
  119.     0, 0,  1, 0, -1, 1,  0, 1, //
  120.   0, 0, -1, 0,  0, 1,  1, 1, //
  121.  -1, 1,  0, 1,  1, 1,  2, 1, // Langer Vierer
  122.  -1, 0,  0, 0,  1, 0,  0, 1, // Dreieck
  123.   1, 0, -1, 1,  0, 1,  1, 1, //
  124.  -1, 0,  0, 0,  1, 0,  1, 1, //
  125.  
  126.     0, 0,  0, 1,  1, 1,  1, 0, // Quader
  127.     0, 0,  0, 1,  1, 1,  1, 2, //
  128.   1, 0,  1, 1,  0, 1,  0, 2, //
  129.     0, 0,  0, 1,  0, 2,  0, 3, // Langer Vierer
  130.  -1, 1,  0, 0,  0, 1,  0, 2, // Dreieck
  131.   0, 0,  0, 1,  0, 2,  1, 2, //
  132.   0, 0,  0, 1, -1, 2,  0, 2, //
  133.  
  134.     0, 0,  0, 1,  1, 1,  1, 0, // Quader
  135.     0, 0,  1, 0, -1, 1,  0, 1, //
  136.   0, 0, -1, 0,  0, 1,  1, 1, //
  137.  -1, 1,  0, 1,  1, 1,  2, 1, // Langer Vierer
  138.  -1, 1,  0, 1,  1, 1,  0, 0, // Dreieck
  139.  -1, 0,  0, 0,  1, 0, -1, 1, //
  140.  -1, 0, -1, 1,  0, 1,  1, 1, //
  141. };
  142.  
  143. char playfield [ 80 ][ Length + Firstline + 2 ];
  144.  
  145. //
  146. // Draw_Teil ()
  147. //
  148.  
  149. void Draw_Teil ( ulong teil, ulong x, ulong y, ulong dreh, BOOL write )
  150. {
  151. char *p;
  152.  
  153.     p = teile + ( 8 * ( teil - 1 )) + ( dreh * 7 * 8 );
  154.     CLONL_Color ( teil, 0, STYLE_Inverse );
  155.     CLONL_GotoXY ( x + *(p + 0) , y + *( p + 1) ); FPutC ( Output(), '+' );
  156.     CLONL_GotoXY ( x + *(p + 2) , y + *( p + 3) ); FPutC ( Output(), '+' );
  157.     CLONL_GotoXY ( x + *(p + 4) , y + *( p + 5) ); FPutC ( Output(), '+' );
  158.     CLONL_GotoXY ( x + *(p + 6) , y + *( p + 7) ); FPutC ( Output(), '+' );
  159.     PutStr (""); CLONL_GotoXY ( 1, Firstline );
  160.     CLONL_FlushOutput ();
  161.  
  162.     if ( write )
  163.     {
  164.         playfield [ x + *(p + 0) ][ y + *(p + 1)] = teil;
  165.         playfield [ x + *(p + 2) ][ y + *(p + 3)] = teil;
  166.         playfield [ x + *(p + 4) ][ y + *(p + 5)] = teil;
  167.         playfield [ x + *(p + 6) ][ y + *(p + 7)] = teil;
  168.     }
  169. }
  170.  
  171. //
  172. // Delete_Teil ()
  173. //
  174.  
  175. void Delete_Teil ( ulong teil, ulong x, ulong y, ulong dreh )
  176. {
  177. char *p;
  178.  
  179.     p = teile + ( 8 * ( teil - 1 )) + ( dreh * 7 * 8 );
  180.     PutStr ("");
  181.     CLONL_GotoXY ( x + *(p + 0) , y + *( p + 1) ); FPutC ( Output(), ' ' );
  182.     CLONL_GotoXY ( x + *(p + 2) , y + *( p + 3) ); FPutC ( Output(), ' ' );
  183.     CLONL_GotoXY ( x + *(p + 4) , y + *( p + 5) ); FPutC ( Output(), ' ' );
  184.     CLONL_GotoXY ( x + *(p + 6) , y + *( p + 7) ); FPutC ( Output(), ' ' );
  185. }
  186.  
  187. //
  188. // Check_Collision ()
  189. //
  190.  
  191. BOOL Check_Collision ( ulong teil, ulong x, ulong y, ulong dreh )
  192. {
  193. char *p;
  194.  
  195.     p = teile + ( 8 * ( teil - 1 )) + ( dreh * 7 * 8 );
  196.     if ( playfield [ x + *(p + 0) ][ y + *(p + 1)] == 0
  197.         && playfield [ x + *(p + 2) ][ y + *(p + 3)] == 0
  198.         && playfield [ x + *(p + 4) ][ y + *(p + 5)] == 0
  199.         && playfield [ x + *(p + 6) ][ y + *(p + 7)] == 0 )
  200.     {
  201.         return TRUE;
  202.     }
  203.     else
  204.     {
  205.         return FALSE;
  206.     }
  207. }
  208.  
  209. //
  210. // Check_Lines ()
  211. //
  212.  
  213. ulong Check_Lines ( ulong start )
  214. {
  215. ulong x, y;
  216. ulong intx, inty;
  217. BOOL is;
  218. ulong howmuch;
  219.  
  220.     howmuch = 0;
  221.     for ( y = start; y < Firstline + Length; y ++ )
  222.     {
  223.         is = TRUE;
  224.         for ( x = Begin; x < ( Begin + Width ); x ++ )
  225.         {
  226.             if ( playfield [ x ][ y ] == 0 ) is = FALSE;
  227.         }
  228.         if ( is )
  229.         {
  230.             howmuch ++; PutStr ("");
  231.             CLONL_GotoXY ( 1, y         ); CLONL_DeleteLine ();
  232.             CLONL_GotoXY ( 1, Firstline ); CLONL_InsertLine ();
  233.             CLONL_Color ( COLOR_Blue, COLOR_White, STYLE_Inverse );
  234.             CLONL_GotoXY ( Begin - 1,     Firstline ); FPutC ( Output(), '|' );
  235.             CLONL_GotoXY ( Begin + Width, Firstline ); FPutC ( Output(), '|' );
  236.  
  237.             for ( inty = y ; inty > Firstline; inty -- )
  238.             {
  239.                 for ( intx = Begin; intx < ( Begin + Width ); intx ++ )
  240.                 {
  241.                     playfield [ intx ][ inty ] = playfield [ intx ][ inty - 1 ];
  242.                 }
  243.             }
  244.             for ( intx = Begin; intx < ( Begin + Width ); intx ++ )
  245.             {
  246.                 playfield [ intx ][ Firstline ] = 0;
  247.             }
  248.         }
  249.     }
  250.     return howmuch;
  251. }
  252.  
  253. //
  254. // Spiel ()
  255. //
  256.  
  257. ulong Spiel ( void )
  258. {
  259. ulong teil, x, y, delay, count, dreh, nextteil;
  260. ulong oldx, oldy, score, teilcount;
  261. BOOL falling, work, first;
  262. long key;
  263.  
  264.     CLONL_DrawTitle ();
  265.     Draw_Playfield ();
  266.  
  267.     score = 0;
  268.  
  269.     // Spielfeld löschen
  270.     for ( y = 0; y < ( Length + Firstline + 2 ); y ++ )
  271.     {
  272.         for ( x = 0; x < 80; x ++ )
  273.         {
  274.             if ( y == ( Length + Firstline ) )
  275.                 playfield [ x ][ y ] = 1;
  276.             else                
  277.                 if ( x >= Begin && x < ( Begin + Width ) )
  278.                     playfield [ x ][ y ] = 0;
  279.                 else
  280.                     playfield [ x ][ y ] = 1;
  281.         }
  282.     }
  283.     while ( !( teil = rand () & 7 ) ); // schlechter Zufall, durch time_t ersetzen ?!
  284.     delay = 30;
  285.     teilcount = 0;
  286.     work = TRUE;
  287.  
  288.     while ( work )
  289.     {
  290.         while ( !( nextteil = rand () & 7 ) ); // schlechter Zufall, durch time_t ersetzen ?!
  291.  
  292.         CLONL_GotoXY ( NextX, NextY ); PutMSG ("MSG_NEXT");
  293.         CLONL_GotoXY ( 1, Firstline - 1 ); CLONL_Printf ( MSG("MSG_SCORE"), score );
  294.         Draw_Teil ( nextteil, NextX + 1, NextY + 2, 0, FALSE );    
  295.  
  296.         x = Begin + ( Width / 2 );
  297.         y = Firstline;
  298.         oldx = x; oldy = y;
  299.         falling = TRUE;
  300.         dreh = 0;
  301.         first = TRUE;
  302.  
  303.         while ( falling )
  304.         {
  305.             if ( Check_Collision ( teil, x, y, dreh ) )
  306.             {
  307.                 first = FALSE;
  308.                 Draw_Teil ( teil, x, y, dreh, FALSE );
  309.                 for ( count = 0; count < delay; count ++ )
  310.                 {
  311.                     Delay ( 1 );
  312.                     if ( CLONL_IsKeyPressed() )
  313.                     {
  314.                         switch ( CLONL_GetKey () )
  315.                         {
  316.                             case CHAR_EOF:
  317.                             case CHAR_BREAK:
  318.                                 return score;
  319.                                 break;
  320.                             case CHAR_DOWN:
  321.                                 score += 1;
  322.                                 count = delay;
  323.                                 break;
  324.                             case CHAR_LEFT:
  325.                                 if ( Check_Collision ( teil, x - 1, y, dreh ) )
  326.                                 {
  327.                                     Delete_Teil ( teil, x, y, dreh );
  328.                                     x -= 1;
  329.                                     Draw_Teil ( teil, x, y, dreh, FALSE );
  330.                                 }
  331.                                 break;
  332.                             case CHAR_RIGHT:
  333.                                 if ( Check_Collision ( teil, x + 1, y, dreh ) )
  334.                                 {
  335.                                     Delete_Teil ( teil, x, y, dreh );
  336.                                     x += 1;
  337.                                     Draw_Teil ( teil, x, y, dreh, FALSE );
  338.                                 }
  339.                                 break;
  340.                             case ' ':
  341.                             case CHAR_UP:
  342.                                 if ( Check_Collision ( teil, x, y, (dreh + 1) & 3 ) )
  343.                                 {
  344.                                     Delete_Teil ( teil, x, y, dreh );
  345.                                     dreh = (dreh + 1) & 3;
  346.                                     Draw_Teil ( teil, x, y, dreh, FALSE );
  347.                                 }
  348.                                 break;
  349.                         }
  350.                     }
  351.                 }
  352.                 Delete_Teil ( teil, x, y, dreh );
  353.                 oldx = x; oldy = y;
  354.                 y += 1;
  355.             }
  356.             else
  357.             {
  358.                 while ( CLONL_IsKeyPressed() )
  359.                 {
  360.                     key = CLONL_GetKey();
  361.                     if ( key == CHAR_BREAK || key == CHAR_EOF ) return score;
  362.                 }
  363.                 Draw_Teil ( teil, oldx, oldy, dreh, TRUE );
  364.                 if ( first ) 
  365.                 {
  366.                     work = FALSE;
  367.                     falling = FALSE;
  368.                 }
  369.                 else
  370.                 {
  371.                     teilcount ++;
  372.                     if ( teilcount >= 40 )
  373.                     {
  374.                         if ( delay > 1 ) delay --;
  375.                     }
  376.                     score += 10;
  377.                     CLONL_GotoXY ( NextX, NextY ); CLONL_DeleteEOL ();
  378.                     Delete_Teil ( nextteil, NextX + 1, NextY + 2, 0 );    
  379.                     score += ( Check_Lines ( oldy ) * 100 );
  380.                     falling = FALSE;
  381.                     teil = nextteil;
  382.                 }
  383.             }
  384.         }
  385.     }
  386.     Delay ( 120 );    
  387.     return score;
  388. }
  389.  
  390. //
  391. // Sortin_Score()
  392. //
  393.  
  394. void Sortin_Score ( ulong score, char *name )
  395. {
  396. BPTR file;
  397. char spieler [ 11 ][ 40 ];
  398. ulong punkte [ 11 ];
  399. ulong count, pos;
  400. char line [ 82 ];
  401. BOOL nicht;
  402.  
  403.     nicht = TRUE;
  404.  
  405.     if ( file = Open ( HighscoreFilename, MODE_OLDFILE ) )
  406.     {
  407.         pos = 0;
  408.         for ( count = 0; count < 10; count ++ )
  409.         {
  410.             FGets ( file, line, 80 );
  411.             line [ strlen ( line ) - 1 ] = 0; // LF entfernen
  412.             sscanf ( line, "%d %s", &punkte [ pos ], spieler [ pos ] );
  413.  
  414.             if ( nicht && strcmp ( spieler [ pos ], name ) == 0 && punkte [ pos ] >= score ) break;
  415.             if ( nicht && punkte [ pos ] < score ) 
  416.             { 
  417.                 strcpy ( spieler [ pos + 1 ], spieler [ pos ] );
  418.                 punkte [ pos + 1 ] = punkte [ pos ];
  419.                 strcpy ( spieler [ pos ], name );
  420.                 punkte [ pos ] = score;
  421.                 pos ++; 
  422.                 nicht = FALSE; 
  423.             }
  424.             if ( strcmp ( spieler [ pos ], name ) == 0 ) pos --;
  425.             pos++;
  426.         }
  427.         Close ( file );
  428.     }
  429.     else
  430.     {
  431.         PutStr ("Can't open highscore-file.\n");
  432.         return;
  433.     }
  434.  
  435.     if (!nicht)
  436.     {
  437.         if ( file = Open ( HighscoreFilename, MODE_NEWFILE ) )
  438.         {
  439.             for ( count = 0; count < 10; count ++ )
  440.             {
  441.                 FPrintf ( file, "%ld %s\n", punkte [ count ], spieler [ count ] );
  442.             }
  443.             Close ( file );
  444.         }
  445.         else
  446.         {
  447.             PutStr ("Can't open highscore-file.\n");
  448.             return;
  449.         }
  450.     }
  451. }
  452.  
  453. //
  454. // main
  455. //
  456.  
  457. int main ( int argc, char *argv[] )
  458. {
  459. struct Shortmenu *mymenu;
  460. char key;
  461. BOOL quit = FALSE;
  462. ulong score;
  463.  
  464.     if ( !CLONL_Open ( "Online_Connectris.clcat" ) ) { PutStr ( "Can't init ANSI-System.\n" ); return 20; }
  465.  
  466.     if ( CLONL_IsTTY () ) { PutStr ("You need at least VT100 or ANSI Terminal to run.\n" ); goto Ende; }
  467.  
  468.     mymenu = CLONL_AddShortMenu ( NULL, MSG("MSG_SPIELEN"), MSG("MSG_SPIELENHOT")[ 0 ] );
  469.     mymenu = CLONL_AddShortMenu ( mymenu, MSG("MSG_QUIT"), MSG("MSG_QUITHOT")[ 0 ] );
  470.  
  471.     while ( !quit )
  472.     {
  473.       CLONL_DrawTitle ();
  474.         PutStr ("Copyright 1995 by Mathias Mischler\n\n");
  475.         Draw_Highscore ();
  476.  
  477.         key = CLONL_ShortMenuSelect ( mymenu );
  478.  
  479.         switch ( key )
  480.         {
  481.             case 'Q':
  482.             case 0:
  483.                 quit = TRUE;
  484.                 break;
  485.             case 'S':
  486.                 srand ( time ( 0 ) );
  487.                 score = Spiel ();
  488.                 Sortin_Score ( score, CLONL_GetUserShell()->user.Username );
  489.                 break;
  490.         }
  491.     }
  492.     
  493.     PutStr ("\n");
  494.     CLONL_FreeShortMenu ( mymenu );
  495.     Ende:
  496.     CLONL_Close ();
  497. }
  498.